# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/2/15
# @File   : R27_IC_NonExternalGuaranteeAssetsDisposalAndBadDebtsAndGuarantorCompensationCheck.py
# @E-mail : zhaochen@bbdservice.com


'''eof
name:申请人5年内信贷业务资产处置、呆账、保证人代偿笔数校验
code:R27_IC_NonExternalGuaranteeAssetsDisposalAndBadDebtsAndGuarantorCompensationCheck
tableName:
columnName:
groups:个人征信校验
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = IC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['reportNo'] in null_type_list:
                return [{}]
            # 存在空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):

    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False
# 处理贷记卡信息 和 准贷记卡信息
def loanInfo002Pre(tableName):
    table = getTable(table_Name=tableName)
    if table is None:
        return None
    elif table == [{}]:
        return 0
    else:
        Factor = 0
        flag = []
        for i in table:
            try:
                if i['state']  == u'呆账':
                    Factor += 1
            except:
                flag.append(None)
                continue
        if len(flag) == len(table):
            return None
        else:
            return Factor

# 统计资产处置汇总表 和 保证人代偿汇总表 的条数
def countAssetOrAssurer(tableName):
    table = getTable(table_Name=tableName)
    if table is None:
        return None
    elif table == [{}]:
        return 0
    else:
        count = 0
        flag = []
        for i in table:
            try:
                if i['reportNo'] not in null_type_list:
                    count += 1
            except:
                flag.append(None)
                continue
        if len(flag) == len(table):
            return None
        else:
            return count

def R27_IC_NonExternalGuaranteeAssetsDisposalAndBadDebtsAndGuarantorCompensationCheck():
    #呆账、资产处置、保证人代偿信息概要（ICRFellbackSummary）
    ICRFellbackSummary = getTable('ICRFellbackSummary')
    if ICRFellbackSummary is None:
        count = None
        count2 = None
        count3 = None
    elif ICRFellbackSummary == [{}]:
        count = 0
        count2 = 0
        count3 = 0
    else:
        try:
            count = float(ICRFellbackSummary[0]['count'])
        except:
            count = None
        try:
            count2 = float(ICRFellbackSummary[0]['count2'])
        except:
            count2 = None
        try:
            count3 = float(ICRFellbackSummary[0]['count3'])
        except:
            count3 = None

    # Factor1处理
    cardResult = loanInfo002Pre('ICRLoancardInfo002*Card')
    stdResult = loanInfo002Pre('ICRStdLoancardInfo002*Std')
    if cardResult is None and stdResult is None:
        Factor11 = None
    else:
        if cardResult is None:
            cardResult = 0
        if stdResult is None:
            stdResult = 0
        Factor11 = cardResult + stdResult
    if Factor11 is None and count is None:
        Factor1 = None
    else:
        if Factor11 is None:
            Factor11 = 0
        if count is None:
            count = 0
        Factor1 = max(Factor11, count)

    # Factor2处理
    assetResult = countAssetOrAssurer('ICRAssetDisposition')
    if assetResult is None and count2 is None:
        Factor2 = None
    else:
        if assetResult is None:
            assetResult = 0
        if count2 is None:
            count2 = 0
        Factor2 = max(assetResult, count2)

    # Factor3处理
    assurerResult = countAssetOrAssurer('ICRAssurerRepay')
    if assurerResult is None and count3 is None:
        Factor3 = None
    else:
        if assurerResult is None:
            assurerResult = 0
        if count3 is None:
            count3 = 0
        Factor3 = max(assurerResult, count3)
    # 处理Factor1 Factor2 Factor3
    if Factor1 is None and Factor2 is None and Factor3 is None:
        return u'缺失值'
    else:
        if Factor1 is None:
            Factor1 = 0
        if Factor2 is None:
            Factor2 = 0
        if Factor3 is None:
            Factor3 = 0
        return Factor1 + Factor2 + Factor3

result = R27_IC_NonExternalGuaranteeAssetsDisposalAndBadDebtsAndGuarantorCompensationCheck()